<?php
	session_start();
	require_once('common/config.php');
	require_once('../_helper/paging.php');
	require_once('../_helper/helper.php');
	require_once('../_model/type_model.php');
	require_once('../_model/cat_model.php');
	
	$pageCMS = PAGE_CMS_TYPE;
	
	$cat_id = '';
	$keyword = '';
	if(!isset($_SESSION['logon']))
	{
		header("Location: /wks/login.php?url=brand");
		exit();
	}
	
	if (!isset($_GET['action']))
		$_GET['action'] = "list";
		
	if (isset($_GET['id'])){
		$type_id = $_GET['id'];
	}
	if (isset($_GET['keyword'])){
		$keyword = $_GET['keyword'];
	}
	if (isset($_GET['cat_id'])){
		$cat_id = $_GET['cat_id'];
	}

	$action = $_GET['action'];
	
	if($action == 'search' || $action == 'list')
	{
		if(isset($_GET['page']))
			$curPage = $_GET['page'];
		else
		{
			$curPage = 0;
			unset($_SESSION['numType']);
		}
			
		$type_arr = GetTypeSearch($curPage, PAGING_NUM_ROW, $cat_id, $keyword);
		/*var_dump($type_arr);die();*/
		if( !isset($_SESSION['numType']))
		{
			$numType = GetNumTypeSearch($cat_id, $keyword);
			$_SESSION['numType'] = $numType;
		}
		else
			$numType = $_SESSION['numType'];
			
		$param = "action=search&cat_id={$cat_id}&keyword={$keyword}";
		/*var_dump($_SESSION['numType']);*/
		
		$cat_arr = GetCatList();
		//Xu ly
		if(isset($_GET['error']))
			$isError = TRUE;
		else
			$isError = FALSE;
		require_once('template/type/type_list.php');
		exit();
	}
	
	if($action == 'add')
	{
		if(!isset($_POST['submit'])){
			
			$cat_arr = GetCatList();
			require_once('template/type/type_add.php');
			exit();
		}
		else{
			
			if($_POST['type_name'] != '' && $_POST['cat_id'] != '')
			{
				$order   = array("\r\n", "\n", "\r", "\t", "  ");
				$_POST['type_info_short'] = str_replace($order,'',$_POST['type_info_short']);
				$_POST['type_info_full'] = str_replace($order,'',$_POST['type_info_full']);
				
				InsertType($_POST);
				header("Location: /wks/type.php?action=list");
			}
			else
				header("Location: /wks/type.php?action=list&error=1&e_action=add");
		}
		exit();
	}
	
	if($action == 'edit')
	{
		if (!isset($type_id))
			header("Location: /wks/type.php");		
			
		if(!isset($_POST['submit'])){
			$type_value = GetTypeById($type_id);
			$cat_arr = GetCatList();
			require_once('template/type/type_edit.php');
			exit();
		}
		else{
			if($_POST['type_name'] != '')
			{
				$order   = array("\r\n", "\n", "\r", "\t", "  ");
				$_POST['type_info_short'] = str_replace($order,'',$_POST['type_info_short']);
				$_POST['type_info_full'] = str_replace($order,'',$_POST['type_info_full']);
				
				EditType($type_id, $_POST);
				header("Location: /wks/type.php?action=list");
			}
		}
		exit();
	}
	
	if($action == 'del')
	{
		$result = FALSE;
		if (isset($type_id))
			$result = DeleteType($type_id);
			
		if($result == FALSE)
			header("Location: /wks/type.php?error=1&e_action=del");	
		else
			header("Location: /wks/type.php");	
	}
	

?>


